var config = require("./config.js");
var storage = require('./storage.js')
var token_time = storage.getStorage('token_destroy_time')
// import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

function login(Request) {
    wx.login({
        success: function (res) {
            if (res.code) {
                console.log(res)
                var code = res.code
                wx.request({
                    url: config.domain + '/User/login',
                    data: {code},
                    method: 'GET',
                    success: function (res) {
                        if (res.data.code == 1) {
                            var user_id = res.data.data.user_id
                            var token = res.data.data.token
                            storage.setStorage('user_id', user_id)
                            storage.setStorage('token', token)
                            if (Request.wxLoginCallBack) {Request.wxLoginCallBack()}
                        }
                    }
                })
            }
        }
    })
}


class request {
    constructor() {
        // console.log(token)
        this._baseUrl = config.domain;
        this._token = wx.getStorageSync('token');
        this._header = { 'Content-Type': 'application/x-www-form-urlencoded', 'token': this._token }
    }

    /**
     * GET类型的网络请求
     */
    getRequest(url, data) {

        return this.requestAll(url, data, 'GET')
    }

    /**
     * DELETE类型的网络请求
     */
    deleteRequest(url, data) {

        return this.requestAll(url, data, 'DELETE')
    }

    /**
     * PUT类型的网络请求
     */
    putRequest(url, data) {

        return this.requestAll(url, data, 'PUT')
    }

    /**
     * POST类型的网络请求
     */
    postRequest(url, data) {

        return this.requestAll(url, data, 'POST')
    }

    /**
     * 网络请求
     */
    requestAll(url, data, method) {
        var token = storage.getStorage('token')
        if (url != '/User/like' && url != '/Gymnasium/likes' && url != '/Coach/likes') {
            wx.showLoading({ title: '数据加载中',});
        }
        return new Promise((resolve, reject) => {
            wx.request({
                url: this._baseUrl + url,
                data: data,
                header: { 'Content-Type': 'application/x-www-form-urlencoded', 'token': token },
                method: method,
                success: (res => {
                    if (res.data.msg == '请登录后操作' && res.data.code == 0) {
                        this.wxLoginCallBack = function () {
                            token = storage.getStorage('token')
                            wx.request({
                                url: this._baseUrl + url,
                                data: data,
                                header: { 'Content-Type': 'application/x-www-form-urlencoded', 'token': token },
                                method: method,
                                success: (res => {
                                    if (res.statusCode === 200) {
                                        wx.hideLoading();
                                        resolve(res)
                                    } else if (res.statusCode === 500) {
                                        wx.showLoading({title: '服务器错误'})
                                        reject(res)
                                    }
                                }),
                                fail: (res => {reject(res)})
                            })
                        }
                        login(this)
                    } else {
                        if (res.statusCode === 200) {
                            wx.hideLoading()
                            resolve(res)
                        } else if (res.statusCode === 500) {
                            wx.showLoading({title: '服务器错误'});
                            reject(res)
                        }
                    }
                }),
                fail: (err => {reject(err)})
            })
        })
    }
}



export default request